package com.augurit.wf.bi.web;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.BaseAction;
import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.base.extmodel.ExtEditorGridResult;
import com.augurit.common.base.extmodel.ExtResult;
import com.augurit.common.util.ExtUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.wf.bi.service.IWfActElemPrivDefService;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.bi.web.form.WfActElemPrivDefForm;
import com.augurit.wf.bi.web.form.WfBusTemplateForm;
import com.augurit.wf.common.listener.WfInitListener;
import com.augurit.wf.common.service.IWfService;
import com.augurit.wf.common.util.WfUtils;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("serial")
@Results({
	@Result(name = ExtCrudActionSupport.RELOAD, location = "wf-act-elem-priv-def.action", type = "redirect"),
	@Result(name = ExtCrudActionSupport.SUCCESS, location = "/cc/WfActElemPrivDef/WfActElemPrivDef.jsp")
})
public class WfActElemPrivDefAction extends ExtBaseAction implements ModelDriven<ExtEditorGridResult<WfActElemPrivDefForm>>, Preparable {

	@Autowired
	private IWfActElemPrivDefService wfActElemPrivDefService;
	
	@Autowired
	private IWfBusTemplateService wfBusTemplateService;
	
	@Autowired
	private IWfService wfService;

	// 属性
	private ExtEditorGridResult<WfActElemPrivDefForm> result;
	private Page<WfActElemPrivDefForm> page = new Page<WfActElemPrivDefForm>(15);
	
	private Long templateId;
	private Integer version;
	private String viewName = WfActElemPrivDefForm.DB_ZB_VIEW;
	private String activityName;
	
	
	/**
	 * 获取各个流程的页面元素权限
	 * @return
	 * @throws Exception
	 */
	public String getElemWithPriv() throws Exception{
		ExtUtils.initPageFromExtGridParam(getRequest(), page);
		
		WfActElemPrivDefForm form = getParamsFromPage();
		Page<WfActElemPrivDefForm> results = wfBusTemplateService.getElemWithPrivByType(form.getElementType(), page, form);
		if(results != null && results.getResult() != null && results.getResult().size() > 0)
			ExtBaseAction.extRenderGridJson(results);
		else{
			ExtResult result = new ExtResult();
			result.setTotalCount(0);
			result.setResult(new ArrayList<WfActElemPrivDefForm>(1));
			result.setSuccess(true);
			ExtBaseAction.renderJson(result);
		}
		return null;
	}
	
	
	/**
	 * 设置流程节点的版本信息
	 */
	private void updateProcActPrivInAppCache(List<WfActElemPrivDefForm> list){
		
		if(list !=null && list.size() > 0){
			Map<String, WfActElemPrivDefForm> map = (Map<String, WfActElemPrivDefForm>)BaseAction.getApplication().getAttribute(WfInitListener.PRIV_CACHE);
			
			for(WfActElemPrivDefForm form:list){
				String templateCode = wfBusTemplateService.get(form.getTemplateId()).getTemplateCode();
				String key = WfUtils.formatPrivControlKey(form.getElementCode(), templateCode, form.getVersion().toString(), form.getViewName(),form.getActivityName());
				map.remove(key);
				
				if(form.isHidden() || form.isLocked()){
					map.put(key, form);
				}
			}
			
		}
	}
	
	/**
	 * 更新流程结点的权限信息
	 * @return
	 * @throws Exception
	 */
	public String ActElemPrivCreate() throws Exception {
		persistChange();
		return null;
	}
	
	/**
	 * 更新流程结点的权限信息
	 * @return
	 * @throws Exception
	 */
	public String ActElemPrivUpdate() throws Exception {
		persistChange();
		return null;
	}
	
	private String persistChange() throws Exception {
		List<WfActElemPrivDefForm> list = result.getChangedForms(WfActElemPrivDefForm.class);
		boolean isUpdate = wfActElemPrivDefService.updateActElemPriv(list);
		
		if(isUpdate){
			updateProcActPrivInAppCache(list);
			ExtBaseAction.extRenderEditorGridResult(ExtEditorGridResult.getEditorGridUpdateMessage(list), list, true);
		}
		else
			ExtBaseAction.extRenderEditorGridResult(ExtEditorGridResult.getEditorGridUpdateMessage(null), null, true);
		return null;
	}
	
	
	/**
	 * 获取来自页面的参数
	 * @return
	 */
	private WfActElemPrivDefForm getParamsFromPage(){
		WfBusTemplateForm template = wfBusTemplateService.get(templateId);
		if(version == null){
			version = template.getWfDefVersion();
		}
		if( StringUtils.isBlank(activityName) && viewName.equals(WfActElemPrivDefForm.DB_ZB_VIEW) ){
			activityName = wfService.getStartTaskActivityName(WfUtils.convertToProcessDefinitionId(template.getWfDefKey(), version));
		}
		
		WfActElemPrivDefForm form = new WfActElemPrivDefForm();
		form.setElementType(getRequest().getParameter("elementType"));
		form.setTemplateId(templateId);
		form.setVersion(version);
		form.setActivityName(activityName);
		form.setViewName(viewName);
		return form;
	}
	
	
	/**
	 * 准备页面模型
	 */
	protected void prepareModel() throws Exception {
		result = new ExtEditorGridResult<WfActElemPrivDefForm>();
	}
	
	public ExtEditorGridResult<WfActElemPrivDefForm> getModel() {
		return result;
	}
	
	public void prepare() throws Exception {
		prepareModel();
	}

	public Page<WfActElemPrivDefForm> getPage() {
		return page;
	}


	public void setPage(Page<WfActElemPrivDefForm> page) {
		this.page = page;
	}


	public Long getTemplateId() {
		return templateId;
	}


	public void setTemplateId(Long templateId) {
		this.templateId = templateId;
	}


	public Integer getVersion() {
		return version;
	}


	public void setVersion(Integer version) {
		this.version = version;
	}


	public String getViewName() {
		return viewName;
	}


	public void setViewName(String viewName) {
		this.viewName = viewName;
	}


	public String getActivityName() {
		return activityName;
	}


	public void setActivityName(String activityName) {
		this.activityName = activityName;
	}
	
}
